/*
 * TransitTripItem.java
 *
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.core.trip;

import java.awt.Color;
import java.awt.geom.Rectangle2D;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.fpdev.core.basenet.BaseNetwork;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.fpdev.core.basenet.Path;
import org.fpdev.core.transit.Route;
import org.fpdev.core.transit.SubRoute;
import org.fpdev.apps.rtemaster.gui.map.MapCanvas;
import org.fpdev.core.transit.CalendarService;

public class TransitTripItem implements TripItem {

  private SubRoute sub_;
  private CalendarService service_;
  private int run_,  onDLI_,  offDLI_;

  public TransitTripItem(SubRoute sub, CalendarService service, int run, int onDLI, int offDLI) {
    sub_ = sub;
    service_ = service;
    run_ = run;
    onDLI_ = onDLI;
    offDLI_ = offDLI;
  }

  public List<Integer> getLinkIDList() {
    Path subPath = sub_.getPath().subPath(sub_.getPath().getFirstPLIFromDLI(onDLI_), sub_.getPath().getLastPLIFromDLI(offDLI_));
    List<Integer> list = new LinkedList<Integer>();
    Iterator<BLink> pLinks = subPath.getLinks();
    while (pLinks.hasNext()) {
      list.add(pLinks.next().getID());
    }
    return list;
  }

  public String getXml(BaseNetwork net) {
    Path subPath = sub_.getPath().subPath(sub_.getPath().getFirstPLIFromDLI(onDLI_), sub_.getPath().getLastPLIFromDLI(offDLI_));
    String links = "";
    Iterator<BLink> pLinks = subPath.getLinks();
    while (pLinks.hasNext()) {
      links += pLinks.next().getID() + ",";
    }

    return "    <transit cid=\"" + sub_.getRoute().getCombinedID() + "\" name=\"" + getName() + "\" dir=\"" + getDirection() + "\" mode=\"" + getModeCode() + "\" from=\"" + getFrom(net) + "\" to=\"" + getTo(net) + "\" stime=\"" + getStart() + "\" etime=\"" + getEnd() + "\" len=\"" + getLength() + "\">" + links + "</transit>\n";
  }

  public String getText() {
    return "";
  }

  public SubRoute getSubRoute() {
    return sub_;
  }

  public String getFrom(BaseNetwork net) {
    return sub_.getPath().getStop(onDLI_).getNode().getDisplayName(net);
  }

  public String getTo(BaseNetwork net) {
    return sub_.getPath().getStop(offDLI_ + 1).getNode().getDisplayName(net);
  }

  public String getName() {
    Route rte = sub_.getRoute();
    return (rte.getShowID() ? rte.getID() + " " : "") + rte.getName();
  }

  public String getDirection() {
    return sub_.getID().split("_")[0];
  }

  public int getStart() {
    //System.out.println("gS: run="+run_+" onDLI="+onDLI_);
    return sub_.getTime(service_, run_, onDLI_);
  }

  public int getEnd() {
    return sub_.getTime(service_, run_, offDLI_ + 1);
  }

  public int getLength() {
    int startPLI = sub_.getPath().getFirstPLIFromDLI(onDLI_);
    int endPLI = sub_.getPath().getLastPLIFromDLI(offDLI_);
    //System.out.println(startPLI+" "+endPLI);
    int len = 0;
    for (int pli = startPLI; pli <= endPLI; pli++) {
      int add = sub_.getPath().getLink(pli).getLengthFeet();
      //System.out.println("+"+add);
      len += add;
    }
    return len;
  }

  public String getModeCode() {
    switch (sub_.getPath().getType()) {
      case BUS: return "lbus";
      case METRO: return "hrt";
      case LIGHTRAIL: return "lrt";
      case REGIONALRAIL: return "rr";
    }
    return "err";
  }

  public String getItemName() {
    return getModeCode();
  }
    
  public int getType() {
    return TripItem.TRIP_TRANSIT;
  }

  public int subItemCount() {
    return 1;
  }

  public BNode getBoardNode() {
    return sub_.getPath().getStop(onDLI_).getNode();
  }

  public void draw(MapCanvas canvas) {
    Path subPath = sub_.getPath().subPath(sub_.getPath().getFirstPLIFromDLI(onDLI_), sub_.getPath().getLastPLIFromDLI(offDLI_));
    subPath.draw(canvas, Color.white, 9);
    subPath.draw(canvas, sub_.getPath().getType().getColor(), 4, true);
    canvas.getGraphics().setColor(sub_.getPath().getType().getColor());
    canvas.drawPoint(subPath.startNode().getX(), subPath.startNode().getY(), 12);
    canvas.drawPoint(subPath.endNode().getX(), subPath.endNode().getY(), 12);
    canvas.getGraphics().setColor(java.awt.Color.white);
    canvas.drawPoint(subPath.startNode().getX(), subPath.startNode().getY(), 8);
    canvas.drawPoint(subPath.endNode().getX(), subPath.endNode().getY(), 8);
  }

  public Rectangle2D.Double getBoundingBox() {
    return sub_.getPath().subPath(sub_.getPath().getFirstPLIFromDLI(onDLI_), sub_.getPath().getLastPLIFromDLI(offDLI_)).getBoundingBox();
  }
}




























